<?php

/**

 * Config_GeneralConfigObject - Configuration générale de l'application

 *

 * @package

 * @author Alexis Eric, 

 * Erra Prod. http://www.erraprod.com

 * @copyright Copyright (c) 2012 - Erra Prod

 * @access public

 */
class Config_GeneralConfigObject extends Erra_Fields_Base_Object {

    public static $_configObject = null;

    function __construct($objectId, $buildArgs = null) {

        parent::__construct($objectId, $buildArgs);

        $endStr = ' avant le rendez-vous';

        $sms_client_reminder = Erra_Fields_ComboBox::create('sms_client_reminder')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription('Envoyer un rappel SMS au client')
                ->setDatabaseType('TINYINT')
                ->fill(array(0 => 'Aucun rappel', 2 => '2 heures' . $endStr, 3 => '3 heures' . $endStr, 4 => '4 heures' . $endStr,
            5 => '5 heures' . $endStr, 6 => '6 heures' . $endStr, 9 => '9 heures' . $endStr, 12 => '12 heures' . $endStr, 18 => '18 heures' . $endStr,
            24 => '24 heures' . $endStr, 48 => '48 heures' . $endStr));





        $mail_client_reminder = Erra_Fields_ComboBox::create('mail_client_reminder')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription('Envoyer un e-mail')
                ->setDatabaseType('TINYINT')
                ->fill(array(0 => 'Aucun rappel', 2 => '2 heures' . $endStr, 3 => '3 heures' . $endStr, 4 => '4 heures' . $endStr,
            5 => '5 heures' . $endStr, 6 => '6 heures' . $endStr, 9 => '9 heures' . $endStr, 12 => '12 heures' . $endStr, 18 => '18 heures' . $endStr,
            24 => '24 heures' . $endStr, 48 => '48 heures' . $endStr));



        $sms_client_newrdv = Erra_Fields_ComboBox::create('sms_client_newrdv')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription('Envoyer une confirmation du rendez-vous par SMS')
                ->setDatabaseType('TINYINT')
                ->fill(array(0 => 'Non', 1 => 'Oui'));



        $sms_client_deleterdv = Erra_Fields_ComboBox::create('sms_client_deleterdv')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription('Envoyer un message SMS pour la suppression d\'un rendez-vous')
                ->setDatabaseType('TINYINT')
                ->fill(array(0 => 'Non', 1 => 'Oui'));



        $sms_team_reminder = Erra_Fields_ComboBox::create('sms_team_reminder')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription('Envoyer un rappel SMS à vos équipes')
                ->setDatabaseType('TINYINT')
                ->fill(array(0 => 'Aucun rappel', 2 => '2 heures' . $endStr, 3 => '3 heures' . $endStr, 4 => '4 heures' . $endStr,
            5 => '5 heures' . $endStr, 6 => '6 heures' . $endStr, 9 => '9 heures' . $endStr, 12 => '12 heures' . $endStr, 18 => '18 heures' . $endStr,
            24 => '24 heures' . $endStr, 48 => '48 heures' . $endStr));





        $mail_team_reminder = Erra_Fields_ComboBox::create('mail_team_reminder')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription('Envoyer un rappel e-mail à vos équipes')
                ->setDatabaseType('TINYINT')
                ->fill(array(0 => 'Aucun rappel', 2 => '2 heures' . $endStr, 3 => '3 heures' . $endStr, 4 => '4 heures' . $endStr,
            5 => '5 heures' . $endStr, 6 => '6 heures' . $endStr, 9 => '9 heures' . $endStr, 12 => '12 heures' . $endStr, 18 => '18 heures' . $endStr,
            24 => '24 heures' . $endStr, 48 => '48 heures' . $endStr));





        $identifiant_sms = Erra_Fields_Text::create('identifiant_sms')
                ->addAttribute('data-genre', 'general_element')
                ->setDatabaseType('VARCHAR', 128)
                ->setDescription('Identifiant compte SMS');



        $mot_de_passe_sms = Erra_Fields_Text::create('mot_de_passe_sms')
                ->addAttribute('data-genre', 'general_element')
                ->setDatabaseType('VARCHAR', 128)
                ->setDescription('Mot de passe compte SMS');



        $mailer_name = Erra_Fields_Text::create('mailer_name')
                ->addAttribute('data-genre', 'general_element')
                ->setDatabaseType('VARCHAR', 256)
                ->setDescription("Nom d'expediteur pour les e-mail");



        $mailer_address = Erra_Fields_Text::create('mailer_address')
                ->addAttribute('data-genre', 'general_element')
                ->setDatabaseType('VARCHAR', 256)
                ->setDescription('Adresse expediteur pour les e-mail');



        $nom_etablissement = Erra_Fields_Text::create('nom_etablissement')
                ->addAttribute('data-genre', 'general_element')
                ->setDatabaseType('VARCHAR', 256)
                ->setDescription("Nom de l'établissement");



        $url_reservation = Erra_Fields_Text::create('url_reservation')
                ->addAttribute('data-genre', 'general_element')
                ->setDatabaseType('VARCHAR', 256)
                ->setDescription('URL de redirection après la reservation');



        $sms_cost = Erra_Fields_Text::create('sms_cost')
                ->addAttribute('data-genre', 'general_element')
                ->setDescription("Coût unitaire du SMS, en euros (ex: 0.15)")
                ->setDatabaseType('VARCHAR', 8)
                ->setValue(0.15);



        $sms_sendername = Erra_Fields_Text::create('sms_sendername')
                ->addAttribute('data-genre', 'general_element')
                ->setDescription("Nom d'envoi pour les SMS")
                ->setDatabaseType('VARCHAR', 64);



        $show_notifications = Erra_Fields_ComboBox::create('show_notifications')
                ->addAttribute('data-genre', 'general_element')
                ->setDescription("Afficher un popup si le statut des prestations<br/> n'est pas correctement rempli")
                ->setDatabaseType('TINYINT')
                ->fill(array(1 => 'Oui', 2 => 'Non'));



        $calendar_refresh = Erra_Fields_ComboBox::create('calendar_refresh')
                ->addAttribute('data-genre', 'general_element')
                ->setDescription("Délai de rafraichissement du calendrier")
                ->setDatabaseType('TINYINT')
                ->fill(array(10 => '10 secondes', 15 => '15 secondes', 20 => '20 secondes', 30 => '30 secondes', 40 => '40 secondes', 50 => '50 secondes', 60 => '1 minute'))
                ->setValue(30);



        $version = Erra_Fields_Text::create('version')
                ->addAttribute('data-genre', 'general_element')
                ->setDescription("Version du logiciel My Simply Agenda")
                ->setDatabaseType('VARCHAR', 64);



        $duree_intervalle = Erra_Fields_ComboBox::create('duree_intervalle')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription('Durée des intervalles sur le front-office')
                ->setDatabaseType('TINYINT')
                ->fill(array(5 => '5 minutes', 10 => '10 minutes', 15 => '15 minutes', 20 => '20 minutes', 25 => '25 minutes', 30 => '30 minutes'));



        $mail_team_newrdv = Erra_Fields_ComboBox::create('mail_team_newrdv')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription('Envoyer un mail pour les nouveaux rendez-vous')
                ->setDatabaseType('TINYINT')
                ->fill(array(0 => 'Non', 1 => 'Oui'));



        $mail_team_deleterdv = Erra_Fields_ComboBox::create('mail_team_deleterdv')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription("Envoyer un mail pour la suppression d'un rendez-vous")
                ->setDatabaseType('TINYINT')
                ->fill(array(0 => 'Non', 1 => 'Oui'));



        $mail_team_moverdv = Erra_Fields_ComboBox::create('mail_team_moverdv')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription('Envoyer un mail pour le deplacement des rdv')
                ->setDatabaseType('TINYINT')
                ->fill(array(0 => 'Non', 1 => 'Oui'));



        $mail_internetonly = Erra_Fields_ComboBox::create('mail_internetonly')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription('Envoyer des mails seulement pour les rendez-vous internet')
                ->setDatabaseType('TINYINT')
                ->fill(array(0 => 'Non', 1 => 'Oui'));



        $sms_team_newrdv = Erra_Fields_ComboBox::create('sms_team_newrdv')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription('Envoyer un sms pour les nouveaux rendez-vous')
                ->setDatabaseType('TINYINT')
                ->fill(array(0 => 'Non', 1 => 'Oui'));



        $sms_team_deleterdv = Erra_Fields_ComboBox::create('sms_team_deleterdv')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription("Envoyer un sms pour la suppression d'un rendez-vous")
                ->setDatabaseType('TINYINT')
                ->fill(array(0 => 'Non', 1 => 'Oui'));



        $sms_team_moverdv = Erra_Fields_ComboBox::create('sms_team_moverdv')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription('Envoyer un sms pour le deplacement des rdv')
                ->setDatabaseType('TINYINT')
                ->fill(array(0 => 'Non', 1 => 'Oui'));



        $sms_client_moverdv = Erra_Fields_ComboBox::create('sms_client_moverdv')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription('Envoyer un sms pour le deplacement des rdv')
                ->setDatabaseType('TINYINT')
                ->fill(array(0 => 'Non', 1 => 'Oui'));



        $sms_team_internetonly = Erra_Fields_ComboBox::create('sms_team_internetonly')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription(' Envoyer des sms seulement pour les rendez-vous internet')
                ->setDatabaseType('TINYINT')
                ->fill(array(0 => 'Non', 1 => 'Oui'));





        $paypal_vendor_id = Erra_Fields_Text::create('paypal_vendor_id')
                ->addAttribute('data-genre', 'general_element')
                ->setDescription("Adresse e-mail de votre compte Paypal")
                ->setDatabaseType('VARCHAR', 256);



        $paypal_mode = Erra_Fields_ComboBox::create('paypal_mode')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription('Mode de fonctionnement de PayPal')
                ->setDatabaseType('TINYINT')
                ->fill(array(0 => 'Desactivé', 1 => 'Obligatoire', 2 => 'Optionnel'));



        $choose_presta_on_calendar = Erra_Fields_ComboBox::create('choose_presta_on_calendar')
                ->addAttribute('class', 'pointer live-change')
                ->addAttribute('data-genre', 'general_element')
                ->addAttribute('data-function', 'ajax_config_element')
                ->setDescription('Selection du prestataire sur le calendrier')
                ->setDatabaseType('TINYINT')
                ->fill(array(0 => 'Non', 1 => 'Oui'));

        $fuseau_horaire = Erra_Fields_ComboBox::create('fuseau_horaire')
                ->addAttribute('data-genre', 'general_element')
                ->setDescription("Fuseau horaire client")
                ->setDatabaseType('VARCHAR', 64)
                ->fill(array(0 => 'Europe/Paris',))
                ->setValue('Europe/Paris');
        
        $display_price = Erra_Fields_ComboBox::create('display_price')
                ->setDatabaseType('TINYINT', 4)
                ->setDescription('Afficher le prix pour mes clients')
                ->fill(array(1 => 'Oui', 0 => 'Non'));

        $this->addFields($fuseau_horaire, $display_price, $sms_client_reminder, $mail_client_reminder, $sms_client_newrdv, $sms_client_deleterdv, $sms_team_reminder, $mail_team_reminder, $identifiant_sms, $mot_de_passe_sms, $mailer_name, $mailer_address, $nom_etablissement, $url_reservation, $sms_cost, $sms_sendername, $show_notifications, $calendar_refresh, $version, $duree_intervalle, $mail_team_newrdv, $mail_team_deleterdv, $mail_team_moverdv, $mail_internetonly, $sms_team_newrdv, $sms_team_deleterdv, $sms_team_moverdv, $sms_client_moverdv, $sms_team_internetonly, $paypal_vendor_id, $paypal_mode, $choose_presta_on_calendar);

        // DEBUG_ON :
        $this->setSynchronized(true);
        // END_DEBUG
    }

    /**

     * loadConfiguration() - Chargement de l'objet de configuration à partir de la base de donnée

     * Si ce n'est pas possible, un objet est créé pour cet utilisateur avec les valeurs par défaut

     * 

     * @param type $user_id

     * @return type

     */
    public static function loadConfiguration() {

        $configObject = self::$_configObject;

        if ($configObject == null) {

            $configObject = Config_GeneralConfigObject::getObject('general_config_object', 'SQL|CREATE');

            Erra_Core_DatabasePdo::SELECT('SELECT id FROM config_generalconfigobject');

            if (!Erra_Core_DatabasePdo::rowCount()) {

                $configObject->sms_client_reminder->setValue(2);

                $configObject->sms_client_newrdv->setValue(1);

                $configObject->sms_client_deleterdv->setValue(0);

                $configObject->sms_client_moverdv->setValue(0);

                $configObject->mail_client_reminder->setValue(48);

                $configObject->sms_team_reminder->setValue(2);

                $configObject->mail_team_reminder->setValue(4);

                $configObject->identifiant_sms->setValue('0649785510');

                $configObject->mot_de_passe_sms->setValue('0747');

                $configObject->mailer_name->setValue('');

                $configObject->mailer_address->setValue('');

                $configObject->nom_etablissement->setValue('My Simply Agenda');

                $configObject->url_reservation->setValue('http://www.mysimplyagenda.com');

                $configObject->show_notifications->setValue(1);

                $configObject->version->setValue('1300');

                $configObject->duree_intervalle->setValue('10');

                $configObject->mail_team_newrdv->setValue(1);

                $configObject->mail_team_deleterdv->setValue(1);

                $configObject->mail_moverdv->setValue(1);

                $configObject->mail_internetonly->setValue(1);

                $configObject->sms_team_newrdv->setValue(1);

                $configObject->sms_team_deleterdv->setValue(0);

                $configObject->sms_team_moverdv->setValue(0);

                $configObject->sms_team_internetonly->setValue(0);

                $configObject->calendar_refresh->setValue(30);

                $configObject->sms_cost->setValue(0.8);

                $configObject->choose_presta_on_calendar->setValue(1);

                $configObject->paypal_mode->setValue(1);

                $configObject->fuseau_horaire->setValue('Europe/Paris');
                $configObject->display_price->setValue(1);

                Erra_Persistance_PersistanceUnit::saveField($configObject);
            }



            self::$_configObject = $configObject;
        }

        return self::$_configObject;
    }

    /**

     * get() - Retourne un clé de l'objet de configuration

     * 

     * @param type $key - The configuration key

     * @return type

     */
    public static function get($key) {

        $configObject = self::loadConfiguration();

        $return = 'config_option:' . $key;

        if (is_object($configObject) && is_object($configObject->$key)) {

            $return = $configObject->$key->getRawData();
        }

        // We can execute here an extra function (same name as parameter) if database value needs processing before usage

        if (method_exists('Config_GeneralConfigObject', $key)) {

            return Config_GeneralConfigObject::$key($return);
        }

        return $return;
    }

    /**

     * ajaxConfig() - Active l'edition des options de configuration via AJAX

     * 

     * @param type $item_key La clé de configuration

     * @return type

     */
    public static function ajaxConfig($item_key) {

        $configObject = self::loadConfiguration();

        $item_key_function = $item_key . '_';

        if (method_exists('Config_GeneralConfigObject', $item_key_function)) {

            Config_GeneralConfigObject::$item_key_function();
        }

        if (is_object($configObject) && is_object($configObject->$item_key)) {

            return $configObject->$item_key->editInstance();
        }
    }

    /////////////////////////////////////////
    // FONCTIONS PERSONNALISEES
    // Si une valeur de configuration doit être traitée avec d'être renvoyée, vous pouvez créér
    // une fonction homonyme qui sera executée dans get($key) et qui transformera cette valeur
    /////////////////////////////////////////



    /* function ma_cle_a_modifier($valeur_par_defaut) {

      return $valeur_par_defaut;

      } */
}

